package com.str.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.str.dao.DataSplitingDao;
import com.str.dao.KitInfoDao;
import com.str.dao.KitLocusDao;
import com.str.dao.LocusDao;
import com.str.dto.QueryKitOfLocusInfosDto;
import com.str.dto.QueryLocusOfAlleleInfosDto;
import com.str.pojo.DataSpliting;
import com.str.pojo.KitInfo;
import com.str.pojo.KitLocus;
import com.str.pojo.Locus;
import com.str.service.DataStatisticsService;
import com.str.utils.CopyUtils;
import com.str.vo.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.NumberFormat;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Author: ZhangWu, Anything is possible!
 * @CreateTime: 2023/04/19 21:16
 * @Description: 数据统计业务层
 */
@Service
public class DataStatisticsServiceImpl implements DataStatisticsService {


    @Resource
    private KitInfoDao kitInfoDao;

    @Resource
    private KitLocusDao kitLocusDao;

    @Resource
    private LocusDao locusDao;

    @Resource
    private DataSplitingDao dataSplitingDao;
    /**
     * 按试剂盒统计其基因座个数及其占比
     *
     * @param dto
     * @param request
     * @return
     */
    @Override
    public ResponseVO queryKitOfLocusInfos(QueryKitOfLocusInfosDto dto, HttpServletRequest request) {
        QueryKitOfLocusInfosVO vo = new QueryKitOfLocusInfosVO();
        List<KitOfInfosVO> locusInfosVOS = new ArrayList<>();
        List<KitInfo> kitInfos = kitInfoDao.selectList(new QueryWrapper<KitInfo>()
                .like(Objects.nonNull(dto) && StringUtils.isNoneBlank(dto.getKitName()), "kit_name", dto.getKitName())
                .eq("kit_status", 1));
        //数据总数
        int amountTotal = 0;
        if (Objects.nonNull(kitInfos) && kitInfos.size() > 0) {
            for (KitInfo kitInfo : kitInfos) {
                int locusNum = kitLocusDao.selectCount(new QueryWrapper<KitLocus>()
                        .eq("kit_id", kitInfo.getKitId())).intValue();

                KitOfInfosVO kitOfInfosVO = CopyUtils.copyObj(kitInfo, KitOfInfosVO.class);
                kitOfInfosVO.setLocusNum(locusNum);
                amountTotal += locusNum;
                locusInfosVOS.add(kitOfInfosVO);
            }
            //处理数据，计算对应的比例以及合计数据
            float ratioTotal = 0;
            // 创建一个数值格式化对象
            NumberFormat numberFormat = NumberFormat.getInstance();
            // 设置精确到小数点后2位
            numberFormat.setMaximumFractionDigits(2);
            if (Objects.nonNull(locusInfosVOS) && locusInfosVOS.size() > 0) {
                for (KitOfInfosVO locusInfosVO : locusInfosVOS) {
                    if (amountTotal != 0) {
                        float res = (float) locusInfosVO.getLocusNum() / (float) amountTotal;
                        ratioTotal += res;
                        locusInfosVO.setLocusPer(numberFormat.format(res * 100) + "%");
                    }
                }
            }
            List<KitOfInfosVO> collect = locusInfosVOS.stream().sorted(Comparator.comparing(KitOfInfosVO::getLocusNum)).collect(Collectors.toList());
            vo.setLocusInfosVOS(collect);
            vo.setAmountTotal(amountTotal);
            vo.setRatioTotal(numberFormat.format(ratioTotal * 100) + "%");
        }
        return new ResponseVO(vo);
    }

    /**
     * 按试剂盒统计其基因座个数及其占比
     * @param dto
     * @param request
     * @return
     */
    @Override
    public ResponseVO queryLocusOfAlleleInfos(QueryLocusOfAlleleInfosDto dto, HttpServletRequest request) {
        List<QueryLocusOfAlleleInfosVO> vos = new ArrayList<>();
        //查询所有的基因座信息
        List<Locus> locusList = locusDao.selectList(new QueryWrapper<Locus>().eq("locus_status",1));
        if (Objects.nonNull(locusList) && locusList.size()>0){
            for (Locus locus : locusList) {
                QueryLocusOfAlleleInfosVO vo = CopyUtils.copyObj(locus,QueryLocusOfAlleleInfosVO.class);
                List<DataSpliting> dataSplitings = dataSplitingDao.selectList(new QueryWrapper<DataSpliting>().eq("locus_id", locus.getLocusId()));
                if (Objects.nonNull(dataSplitings) && dataSplitings.size()>0){
                    vo.setAlleleListVO(CopyUtils.copyList(dataSplitings,LocusOfAlleleInfosVO.class));
                }
                vos.add(vo);
            }
        }
        return new ResponseVO(vos);
    }

    /**
     * 按组分数统计基因座占比
     * @param request
     * @return
     */
    @Override
    public ResponseVO queryLocusOfComNumber(HttpServletRequest request) {
        LocusOfComNumberVO respVo = new LocusOfComNumberVO();
        QueryLocusOfComNumberVO vo = locusDao.queryLocusOfComNumber();
        // 创建一个数值格式化对象
        NumberFormat numberFormat = NumberFormat.getInstance();
        // 设置精确到小数点后2位
        numberFormat.setMaximumFractionDigits(2);
        return new ResponseVO(vo);
    }
}
